package com.rainwood.material.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.rainwood.material.R;
import com.rainwood.material.bean.ModuleBean;

import java.util.List;

/**
 * @auther : hhx
 * @date :  2021/3/4
 * @des :
 */
public class ModuleAdapter extends RecyclerView.Adapter<ModuleAdapter.ViewHolder> {
    private List<ModuleBean> list;

    public void setList(List<ModuleBean> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_module, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ModuleBean moduleBean = list.get(position);
        holder.ivRes.setBackgroundResource(moduleBean.getRes());
        holder.tvModuleName.setText(moduleBean.getName());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickLister != null) {
                    onItemClickLister.click(position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        AppCompatImageView ivRes;
        AppCompatTextView tvModuleName;



        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivRes = (AppCompatImageView) itemView.findViewById(R.id.iv_res);
            tvModuleName = (AppCompatTextView) itemView.findViewById(R.id.tv_module_name);
        }
    }

    public interface  OnItemClickLister{
        void click(int pos);
    }

    private OnItemClickLister onItemClickLister;

    public void setOnItemClickLister(OnItemClickLister onItemClickLister) {
        this.onItemClickLister = onItemClickLister;
    }
}
